/*
 * ./src/gestion_connection.c
 * Copyright (C) 2009 lameire alexis
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib-2.0/glib.h>
#include "gestion_connection.h"
#include "fonction_connection.h"
#include "trie_commandes.h"

void connection_du_bot(irc_session_t * session, const char *event, const char *origin, const char **params, unsigned int count)
{
	donnee_de_control *jeux = (donnee_de_control *) irc_get_ctx(session);
	printf(_("Connection au serveur réussie.\nTentative de connection au salon.\n"));
	irc_cmd_join(session, jeux->config_prog->chan, NULL);
}

void join_du_bot(irc_session_t * session, const char *event, const char *origin, const char **params, unsigned int count)
{
	printf(_("Quelqu'un vient de se connecter, il s'appelle %s.\n"), origin);
	donnee_de_control *jeux = (donnee_de_control *) irc_get_ctx(session);
	if (strcmp(origin, jeux->config_prog->pseudo) == 0)
	{
		printf(_("Vous êtes dans le salon.\n"));

		//definition du leader potentiel
		strcpy(jeux->pseudo_leader, jeux->config_prog->pseudo_leader);
		jeux->leader = 1;
	}
}

void algo_bot(irc_session_t * session, const char *event, const char *origin, const char **params, unsigned int count)
{
	donnee_de_control *jeux = (donnee_de_control *) irc_get_ctx(session);
	trie_commandes_t *commande = NULL;

	if (strcmp(event, "NOTICE") == 0)
	{
		char *phrase = (char *) g_strdup_printf("<%s> %s\n", origin, params[1]);
		afficher_en_couleur(stdout, BLEU, AUCUNE_COULEUR, phrase);
		g_free(phrase);
	}
	else if (strcmp(event, "PRIVMSG") == 0)
	{
		if (parse_regex("^![_[:lower:]]{1,15}[:space:]", params[1]))
		{
			char *phrase = (char *) g_strdup_printf("<%s> %s\n", origin, params[1]);
			afficher_en_couleur(stdout, JAUNE, FOND_NOIR, phrase);
			g_free(phrase);
		}
		else
		{
			char *phrase = (char *) g_strdup_printf("<%s> %s\n", origin, params[1]);
			afficher_en_couleur(stdout, VERT, AUCUNE_COULEUR, phrase);
			g_free(phrase);
		}
	}

	if (jeux->parti_en_cour && parse_regex(jeux->mot_a_deviner, params[1]))
	{
		valider_pseudo(session, jeux, origin);
	}

	commande = rechercher_commandes(params[1], jeux->commandes, jeux->nb_commandes);
	if (commande != NULL)
	{
		printf("DEBUG : commande %p\n", commande);
		commande->callback(jeux, origin, session, params, commande->taille_cmd);
	}
}

void lister_pseudo(irc_session_t * session, unsigned int event, const char *origin, const char **params, unsigned int count)
{
	donnee_de_control *jeux = (donnee_de_control *) irc_get_ctx(session);
	if (jeux->recherche_pseudo_kick)
	{
		if (event == LIBIRC_RFC_RPL_NAMREPLY)
		{
			if (parse_regex(jeux->pseudo_a_kicker, params[3]))
			{
				if (jeux->recherche_pseudo_ban)
				{
					char *phrase = NULL;
					phrase = (char *) g_strdup_printf("+b %s!*@*", jeux->pseudo_a_kicker);
					irc_cmd_channel_mode(session, jeux->config_prog->chan, phrase);
					g_free(phrase);

					jeux->recherche_pseudo_ban = 0;
				}
				irc_cmd_kick(session, jeux->pseudo_a_kicker, jeux->config_prog->chan, jeux->raison_du_kick);
				free(jeux->pseudo_a_kicker);
				jeux->recherche_pseudo_kick = 0;
			}
			else
			{
				irc_cmd_msg(session, jeux->config_prog->chan, _("Erreur : Pseudo inexistant"));
			}
		}
	}
}
